package com.senseauto.mediacodec_surface;

import android.content.Context;
import android.graphics.PixelFormat;
import android.os.Build;
import android.os.Handler;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.WindowManager;

public class FloatView {
    static String TAG = "FloatView";
    private static Context mContext;
    private static WindowManager mWindowManager;
    private static WindowManager.LayoutParams wmParams;
    private static View mView;
    public static boolean isShow = false;//悬浮框是否已经显示

    private static OnClickListener mListener;//view的点击回调listener
    private static OnFloatClickListener mOnFloatClickListener;


    public void setOnClickListener(OnClickListener listener) {
        mListener = listener;
    }

    public interface OnClickListener {
        void onClick(View view);
    }

    public interface OnFloatClickListener {
        void onCancelClick(View view);

        void onLongClick();

    }
    public boolean isShow(){
        return isShow;
    }

    public void setOnFloatClickListener(OnFloatClickListener onFloatClickListener) {
        mOnFloatClickListener = onFloatClickListener;
    }

    private static FloatView floatView = null;
    public static View mContentView = null;

    private FloatView() {

    }

    public static synchronized FloatView getInstance() {
        if (floatView == null) {
            floatView = new FloatView();
        }
        return floatView;
    }


    /**
     * 是否移动
     */
    private boolean isMove = true;


    /**
     * 显示悬浮框
     */
    public void showFloatView(Context context, int layoutId) {
        mContext = context;

        if (isShow) {
            return;
        }

        mWindowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        wmParams = new WindowManager.LayoutParams();

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            wmParams.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
        } else {
            wmParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
        }


        wmParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
        wmParams.gravity = Gravity.CENTER;
        wmParams.format = PixelFormat.RGBA_8888;
        wmParams.x = context.getResources().getDisplayMetrics().widthPixels;
        wmParams.y = 0;

        wmParams.width = DisplayUtil.dp2px(context, 30);
        wmParams.height = DisplayUtil.dp2px(context, 30);

        mView = LayoutInflater.from(context).inflate(layoutId, null);
        mContentView = mView;
        mView.findViewById(R.id.ivRecord).setOnClickListener(v -> {
            if (mListener != null) {
                mListener.onClick(v);
            }
        });


        mWindowManager.addView(mView, wmParams);


        mView.setOnTouchListener(new View.OnTouchListener() {
            float downX = 0;
            float downY = 0;
            int oddOffsetX = 0;
            int oddOffsetY = 0;

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        downX = event.getX();
                        downY = event.getY();
                        oddOffsetX = wmParams.x;
                        oddOffsetY = wmParams.y;
                        Log.w(TAG, "点击按下悬浮框==downX" + downX + ";downY=" + downY + ";oddOffsetX=" + oddOffsetX + ";oddOffsetY=" + oddOffsetY);
                        handler.postDelayed(runnable, ViewConfiguration.getLongPressTimeout());
                        break;
                    case MotionEvent.ACTION_MOVE:
                        if (!isMove) {
                            break;
                        }
                        float moveX = event.getX();
                        float moveY = event.getY();
                        //不除以3，拖动的view抖动的有点厉害
                        wmParams.x += (moveX - downX) / 3;
                        wmParams.y += (moveY - downY) / 3;
                        Log.w(TAG, "点击“移动”悬浮框==moveX" + moveX + ";moveY=" + moveY + ";wmParams.x=" + wmParams.x + ";wmParams.y=" + wmParams.y);
                        Log.d(TAG, "移动的距离==x:" + Math.abs(moveX - downX) + ";y==" + Math.abs(moveY - downY));
                        int scaledTouchSlop = ViewConfiguration.get(mContext).getScaledTouchSlop();
                        if (Math.abs(moveX - downX) > scaledTouchSlop || Math.abs(moveY - downY) > scaledTouchSlop) { //移动超过了阈值，表示移动了
                            isMove = true; //移除runnable
                            handler.removeCallbacks(runnable);
                            if (mView != null) {

                                mWindowManager.updateViewLayout(mView, wmParams);
                            }
                        }

                        break;
                    case MotionEvent.ACTION_UP:
                        int newOffsetX = wmParams.x;
                        int newOffsetY = wmParams.y;

                        handler.removeCallbacks(runnable);
                        Log.w(TAG, "点击“抬起”悬浮框==newOffsetX=" + newOffsetX + ";newOffsetY=" + newOffsetY + ";oddOffsetX=" + oddOffsetX + ";oddOffsetY=" + oddOffsetY);
                        Log.d(TAG, "移动的距离==x:" + Math.abs(newOffsetX - oddOffsetX) + ";y==" + Math.abs(newOffsetY - oddOffsetY));
                        if (Math.abs(newOffsetX - oddOffsetX) <= 20 || Math.abs(newOffsetY - oddOffsetY) <= 20) {
                            isMove = true;
                            if (mOnFloatClickListener != null) {
                                if (mView != null) {

                                    mWindowManager.updateViewLayout(mView, wmParams);
                                }
                                mOnFloatClickListener.onCancelClick(mView);
                            }
                        }
                        break;
                    case MotionEvent.ACTION_CANCEL:
                        Log.w(TAG, "取消");
                        break;
                }
                return true;
            }

        });

        isShow = true;
    }

    private Runnable runnable = new Runnable() {
        @Override
        public void run() {
            Log.i(TAG, "长按了vvvvv");
            isMove = false;
            if (mOnFloatClickListener != null) {

                mWindowManager.updateViewLayout(mView, wmParams);
                mOnFloatClickListener.onLongClick();
            }
        }
    };

    private Handler handler = new Handler();

    /**
     * 隐藏悬浮窗
     */
    public void hideFloatView() {
        if (mWindowManager != null && isShow) {
            mWindowManager.removeView(mView);
            isShow = false;
        }
    }

} 